package com.ruoyi.system.domain;

/**
 * @author WeiHanQiang
 * @date 2025/05/15 15:33
 **/
import com.ruoyi.system.mapper.SNHelperMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


@Component
public class SNUtil {

    @Autowired
    private SNHelperMapper snHelperDao;

    /**
     * @param tableName  表名 om_sale_order
     * @param snNo       要生成序列号的字段名 so_no
     * @param dateFormat 日期格式 yyyyMMdd
     * @param prefix     前缀 SO
     * @param snLong     流水号位数 4
     * @return
     */
    public String generateSN(String tableName, String snNo, String dateFormat, String prefix, Integer snLong) {
        int maxSN = snHelperDao.getMaxSerialNum(tableName, snNo, snLong);
        String nowDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateFormat));
        StringBuilder sb = new StringBuilder();
        return sb.append(prefix).append(nowDate).append(String.format("%0" + snLong + "d", maxSN)).toString();
    }

    public int getMaxSN(String tableName, String snNo, Integer snLong) {
        return snHelperDao.getMaxSerialNum(tableName, snNo, snLong);
    }



    /**
     * 查询某个id在表中的数量，删除和未删除的都考虑，eg：生成订单明细行号（新的行号就是这个数量+1）
     *
     * @param tableName
     * @param columnName
     * @param idValue
     * @return
     */
    public int getMaxRowNo(String tableName, String columnName, Object idValue) {
        return snHelperDao.getMaxRowNo(tableName, columnName, idValue);
    }

    /**
     * 生成排产编号schedule_no
     * @return
     */
    public static String generateScheduleNo() {
        LocalDateTime currentTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss");
        String formattedTime = currentTime.format(formatter);
        return ("PC-" + formattedTime);
    }
}
